package com.tas.test.service;

import cn.hutool.core.util.RandomUtil;
import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Sorts;
import com.tas.test.pojo.entity.SimpleData;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Init;
import org.noear.solon.annotation.Inject;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

import static com.mongodb.client.model.Projections.fields;
import static com.mongodb.client.model.Projections.include;

/**
 * @Author kevin
 * @Date 2022-06-21 14:06
 * @Description
 */
@Component
public class SimpleDataService {

  @Inject("${mongodb.uri}")
  private String mongoUrl;

  MongoClient mongoClient;
  MongoDatabase database;

  MongoCollection<Document> simpleDataCollection;

  AtomicLong ID = new AtomicLong(0);

  private void initId(){
    BasicDBObject query = new BasicDBObject();
    Bson sort = new Document("_id",-1);
    Bson resultFields = fields(include("_id"));
    Object value = simpleDataCollection.find(query)
        .projection(resultFields).sort(Sorts.orderBy(sort)).limit(1).first();
    if(value instanceof Document doc){
      ID.set(doc.getLong("_id"));
    }
  }
  @Init
  public void init(){
    mongoClient = new MongoClient(new MongoClientURI(mongoUrl));
    database = mongoClient.getDatabase("mongo_test");
    simpleDataCollection = database.getCollection("simple_data");

    initId();
  }
  /**
   * 保存一条随机数据
   * @return
   */
  public Long save(){
    Long id = ID.incrementAndGet();
    Document simpleData = new Document();
    simpleData.put("_id", id);
    simpleData.put("name", RandomUtil.randomString(6));
    simpleData.put("contents",RandomUtil.randomString(100));
    simpleDataCollection.insertOne(simpleData);
    return id;
  }

  /**
   * 随机生成一个id，然后查询大于该id的100条记录
   * @return
   */
  public List<SimpleData> queryList(){
    Long randomId = RandomUtil.randomLong(1, ID.get());
    BasicDBObject query = new BasicDBObject();
//    Bson sort = new Document("_id",-1);
    Bson resultFields = fields(include("_id","name","contents"));
    Bson idParam = new Document("$gte",randomId);
    query.put("markerId", idParam);
    //        Bson query = new Document("markerId", new BsonInt64(6194));

    List<SimpleData> result = new ArrayList<>();
    MongoCursor<Document> documents = simpleDataCollection.find(query)
        .projection(resultFields).limit(100)
//        .sort(Sorts.orderBy(sort))
        .iterator();
    documents.forEachRemaining(document -> {
          SimpleData data = new SimpleData();
          data.setId(document.getLong("_id"));
          data.setName(document.getString("name"));
          data.setContents(document.getString("contents"));
          result.add(data);
        });
    return result;
  }

}
